//
//  PayImformationCell.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/29.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "PayImformationCell.h"
#import "CustomReuseCellView.h"

@interface PayImformationCell()

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) CustomReuseCellView *priceView;

@property (nonatomic, strong) CustomReuseCellView *couponsView;

@end

@implementation PayImformationCell

#pragma mark - ********************生命周期********************

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    static NSString *cellId = @"PayImformationCell";
    PayImformationCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[PayImformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - 内部方法

- (void)initSubView {
    self.backgroundColor = kCOLOR_tableView;
    [self addSubview:self.mainView];
    [self.mainView addSubview:self.priceView];
    [self.mainView addSubview:self.couponsView];
}

- (void)autoLayoutSubviews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    [self.priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@(kUI_CellHeight));
    }];
    [self.couponsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.priceView.mas_bottom);
        make.height.equalTo(self.priceView);
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [[self.couponsView.selectButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (self.selectCouponsCompleted) {
            self.selectCouponsCompleted();
        }
    }];
}

#pragma mark - Setter

- (void)setViewModel:(OrderViewModel *)viewModel {
    _viewModel = viewModel;
    @weakify(self);
    [RACObserve(viewModel, order_coupon) subscribeNext:^(NSString *coupon) {
        @strongify(self);
        NSString *str = ([coupon doubleValue] > 0) ? [NSString stringWithFormat:@"%@元优惠券",coupon] : @"选择优惠券";
        self.couponsView.clickTitle = str;
    }];
    [RACObserve(viewModel, order_packagePrice) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.priceView.value = [NSString stringWithFormat:@"%.2f元", [x floatValue]];
    }];
}

#pragma mark - Setter && Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        [_mainView setViewShadow:kCOLOR_shadow offset:CGSizeMake(0, 0) radius:1];
    }
    return _mainView;
}

- (CustomReuseCellView *)priceView {
    if (!_priceView) {
        _priceView = [CustomReuseCellView new];
        _priceView.iconName = @"dd_package_package";
        _priceView.title = @"套餐价格";
        _priceView.value = @"58";
        _priceView.showLine = YES;
    }
    return _priceView;
}

- (CustomReuseCellView *)couponsView {
    if (!_couponsView) {
        _couponsView = [CustomReuseCellView new];
        _couponsView.iconName = @"dd_package_duration";
        _couponsView.title = @"优惠券";
        _couponsView.cellType = kCustomCellTypeClick;
        _couponsView.clickTitle = @"优惠券";
        _couponsView.showLine = NO;
    }
    return _couponsView;
}

@end
